package com.lly.bigdata.bgayx.controller;

import com.lly.bigdata.bgayx.base.BaseController;
import com.lly.bigdata.bgayx.common.utils.SecurityUtils;
import com.lly.bigdata.bgayx.service.ISysUserService;
import com.lly.bigdata.bgayx.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * JkfwUser的路由接口服务
 * mge/app 只能做为借口服务 用户要不登录 要不用token访问
 *
 * @author
 */
@Api(tags = "用户模块api", value = "SysUserController")
@RestController
@RequestMapping(value = {"/mge/adx"})
public class SysUserController extends BaseController {
    @Autowired
    ISysUserService sysUserService;


    /**
     * @param request, searchModel
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 获取用户信息
     */
    @ApiOperation(value = "获取用户信息列表")
    @RequestMapping("/listUser")
    @PreAuthorize("hasAuthority('sys:post')")
    @ResponseBody
    public AjaxResultList listUser(HttpServletRequest request, SearchModel searchModel) {
        AjaxResultList res = AjaxResultList.ok();
        Result r = sysUserService.queryPage(searchModel);
        res.setRows(r.getList());
        res.setTotal(r.getTotal());
        res.setPages(r.getPages());
        res.setPage((long) searchModel.getPageNumber());
        return res;
    }

    @ApiOperation(value = "获取用户id数据")
    @RequestMapping("getUserid")
    @ResponseBody
    public AjaxResult getUser(String userid) {
        Result user = sysUserService.selectByUserid(userid);
        if (user != null) {
            return AjaxResult.success(user.getData());
        } else {
            return AjaxResult.error("用户不存在！");
        }
    }

    /**
     * 添加用户
     *
     * @param request
     * @param user
     * @return com.bigdata.model.AjaxResult
     * @author lly
     * @date 2020/3/22 18:31
     */
    @ApiOperation(value = "添加用户数据")
    @RequestMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(HttpServletRequest request, SysUserVo user) {
        LoginUser sysUser = SecurityUtils.getLoginUser();
        Result res = sysUserService.addUserRole(user, sysUser.getUserId());
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.error(res.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * @param request, user
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 添加用户和权限
     */
    @ApiOperation(value = "添加用户角色数据")
    @RequestMapping("/addUserRole")
    @ResponseBody
    public AjaxResult addUserRole(HttpServletRequest request, SysUserVo user) {
        LoginUser sysUser = SecurityUtils.getLoginUser();
        Result res = sysUserService.addUserRole(user, sysUser.getUserId());
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.error(res.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 验证用户名是否存在
     *
     * @param username
     * @return
     */
    @ApiOperation(value = "检测用户是否存在")
    @RequestMapping("ckUsername")
    @ResponseBody
    public AjaxResult checkUserName(String username) {
        AjaxResult result = AjaxResult.success(sysUserService.ckUserName(username));
        return result;
    }

    /**
     * @param request, user
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 修改用户
     */
    @ApiOperation(value = "修改用户")
    @PostMapping("/updateUser")
    @ResponseBody
    public AjaxResult updateUser(HttpServletRequest request, SysUserVo user) {
        LoginUser sysUser = SecurityUtils.getLoginUser();
        Result res = sysUserService.updateUser(user, sysUser.getUserId());
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.error(res.getMessage());
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改用户角色")
    @PostMapping("/updateUserRole")
    @ResponseBody
    public AjaxResult updateUserRole(HttpServletRequest request, SysUserVo user) {
        LoginUser sysUser = SecurityUtils.getLoginUser();
        Result res = sysUserService.updateUserRole(user, sysUser.getUserId());
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.error(res.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * @param request, userid
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 删除用户
     */
    @ApiOperation(value = "删除用户")
    @PostMapping("delUser")
    @ResponseBody
    public AjaxResult delUser(HttpServletRequest request, Long userid) {
        if (userid == null) {
            return AjaxResult.error("用户id不能为空！");
        }
        sysUserService.delUser(userid);
        return AjaxResult.success();
    }

}
